package com.guangyuan.network.mvc.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.ArrayList;
import java.util.List;

import com.guangyuan.network.constants.ProductGroupConstant;
import com.guangyuan.network.dataobject.Product;
import com.guangyuan.network.dataobject.ProductAttrRel;
import com.guangyuan.network.external.OperatorFactory;

public class ProductAction implements Action {

	public boolean addProduct(HttpServletRequest request,
			HttpServletResponse response) {
		Product product = getProduct(request, response);

		if (null != product) {
			int id = OperatorFactory.getProductOper().insertProduct(product);
			System.out.println(id);
			if (id <= 0) {

				return false;
			}
			List<ProductAttrRel> attrs = getProductAttrRel(request, response);
			for (ProductAttrRel attr : attrs) {
				attr.setProductId(id);
				OperatorFactory.getProductAttrRelOper().insertProductAttrRel(
						attr);
			}
		}

		return false;
	}

	public boolean modifiedProduct(HttpServletRequest request,
			HttpServletResponse response) {
		Product product = getProduct(request, response);

		if (null != product) {
			boolean result = OperatorFactory.getProductOper().updateProduct(
					product);
			if (!result)
				return false;

			List<ProductAttrRel> attrs = getProductAttrRel(request, response);

			for (ProductAttrRel attr : attrs) {
				attr.setProductId(product.getId());
				OperatorFactory.getProductAttrRelOper().updateProductAttrRel(
						attr);
			}
		}

		return false;
	}

	public boolean deleteProduct(HttpServletRequest request,
			HttpServletResponse response) {
		String product_id = request
				.getParameter(ProductGroupConstant.product_id);

		if (null == product_id || "".equals(product_id)) {
			return false;
		}

		int productId = Integer.parseInt(product_id);
		//FIXME!修改
		OperatorFactory.getProductAttrRelOper().deleteProductAttrRelByProductId(productId);
		return OperatorFactory.getProductOper().deleteProduct(productId);
	}

	private Product getProduct(HttpServletRequest request,
			HttpServletResponse response) {

		Product product = new Product();

		String group_id = request.getParameter(ProductGroupConstant.GROUP_ID);
		int groupId = 0;
		if (null != group_id && !"".equals(group_id)) {
			groupId = Integer.parseInt(group_id);
		}
		String product_id = request
				.getParameter(ProductGroupConstant.product_id);

		int productId = 0;
		if (null != product_id) {
			productId = Integer.parseInt(product_id);
		}
		product.setId(productId);

		product.setProductGroupId(groupId);
		String name = request.getParameter(ProductGroupConstant.product_name);
		String cnName = request
				.getParameter(ProductGroupConstant.product_cnName);
		String description = request
				.getParameter(ProductGroupConstant.product_description);
		// FIXME! 增加对产品图片上传的处理
		product.setName(name);
		product.setCnName(cnName);
		product.setDescription(description);
		product.setOperator("吴天翼");

		return product;
	}

	private List<ProductAttrRel> getProductAttrRel(HttpServletRequest request,
			HttpServletResponse response) {
		String attr_size = request.getParameter(ProductGroupConstant.attr_size);

		int attrSize = 0;
		if (null != attr_size && !"".equals(attr_size)) {
			attrSize = Integer.parseInt(attr_size);
		}
		List<ProductAttrRel> attrs = new ArrayList<ProductAttrRel>();
		for (int i = 1; i <= attrSize; i++) {
			ProductAttrRel attr = new ProductAttrRel();
			int attr_id = Integer.parseInt(request
					.getParameter(ProductGroupConstant.attr_id + "_" + i));
			attr.setProductAttrId(attr_id);

			attr.setValue(request.getParameter(ProductGroupConstant.attr_value
					+ "_" + i));
			// FIXME!设置操作者的名称
			System.out.println(attr_id
					+ " : "
					+ request.getParameter(ProductGroupConstant.attr_value
							+ "_" + i));
			attr.setOperator("吴天翼");
			attrs.add(attr);
		}
		return attrs;
	}

}
